package com.mcxx.modules.extremepoverty.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.extremepoverty.service.ExFuneralExpenseService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.family.dto.ExMemberExcelNewDto;
import com.mcxx.modules.miniSalvation.family.dto.FuneralDTO;
import com.mcxx.modules.miniSalvation.family.dto.FuneralExcelDTO;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayReadService;
import com.mcxx.modules.miniSalvation.order.dto.CheckDTO;
import com.mcxx.modules.miniSalvation.order.entity.SuspendApproveEntity;
import com.mcxx.modules.miniSalvation.order.param.FuneralParam;
import com.mcxx.modules.miniSalvation.order.service.read.FuneralReadService;
import com.mcxx.modules.miniSalvation.order.service.read.OrderCertReadService;
import com.mcxx.modules.miniSalvation.order.service.read.SuspendApproveReadService;
import com.mcxx.modules.miniSalvation.order.service.write.FuneralWriteService;
import com.mcxx.modules.miniSalvation.order.service.write.SuspendApproveWriteService;
import com.mcxx.modules.system.entity.TaskExportEntity;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
@TaTransactional
public class ExFuneralExpenseServiceImpl extends BaseService implements ExFuneralExpenseService {


    @Autowired
    private FileService fileService;

    @Autowired
    private TaskExportWriteService taskExportWriteService;

    @Resource
    private ExportExcelService exportExcelService;

    @Autowired
    private FuneralReadService funeralReadService;
    @Autowired
    private OrderCertReadService orderCertReadService;
    @Autowired
    private FuneralWriteService funeralWriteService;
    @Autowired
    private SuspendApproveWriteService suspendApproveWriteService;
    @Autowired
    private SuspendApproveReadService suspendApproveReadService;
    @Autowired
    private FundPayReadService fundPayReadService;

    @Override
    public ExcelInfo exportExcelFuneral(String exportType, List<String> orderIdList, FuneralParam param, UserAccountVo accountVo) {
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.TEKUN);

        List<FuneralExcelDTO> list = funeralReadService.exportExFuneralList(param);
        excelInfo = exportExcelService.exportOrgOpLogs(FuneralExcelDTO.class,list,"丧葬费维护信息.xls",
                "丧葬费维护列表","丧葬费维护列表",false);


        return excelInfo;
    }


    @Override
    public Map<String, List<String>> sendAudit(List<FuneralDTO> funeralDTOList, UserAccountVo accountVo) {
        Map<String, List<String>> idcardCheckRstMsg = new HashMap<>();
        Map<String, FuneralDTO> idcardCheckDTOMap = funeralDTOList.stream().collect(Collectors.toMap(FuneralDTO::getIdcard, c -> c));
        List<String> canNotSendAuditList = new ArrayList<>();

        for(FuneralDTO funeralDTO: funeralDTOList){
            if(Constant.FuneralStatus.AUDIT.equals(funeralDTO.getStatus())){
                canNotSendAuditList.add(funeralDTO.getIdcard());

            }
        }
        if(CollectionUtils.isNotEmpty(canNotSendAuditList)){
            for(String idcard : canNotSendAuditList){
                funeralDTOList.remove(idcardCheckDTOMap.get(idcard));
            }
            idcardCheckRstMsg.put("canNotSendAudit", canNotSendAuditList);
        }
        if(funeralDTOList.size() == 0){

            return idcardCheckRstMsg;
        }


        int rsts;
        List<String> noExistIdcardCertList = new ArrayList<>();

        for(FuneralDTO funeralDTO: funeralDTOList){
            if(Constant.recevierType.PERSONAL.equals(funeralDTO.getRecevierType())){
                rsts = orderCertReadService.cntCertNum(funeralDTO.getId(),funeralDTO.getOrderId(),"102","12");
                if(rsts <= 0){
                    noExistIdcardCertList.add(funeralDTO.getIdcard());

                }
            }

        }
        if(CollectionUtils.isNotEmpty(noExistIdcardCertList)){
            for(String idcard : noExistIdcardCertList){
                funeralDTOList.remove(idcardCheckDTOMap.get(idcard));
            }
            idcardCheckRstMsg.put("noExistIdcardCert", noExistIdcardCertList);
        }
        if(funeralDTOList.size() == 0){

            return idcardCheckRstMsg;
        }


        List<String> noExistBankCertList = new ArrayList<>();

        for(FuneralDTO funeralDTO: funeralDTOList){
            if(Constant.recevierType.PERSONAL.equals(funeralDTO.getRecevierType())) {
                rsts = orderCertReadService.cntCertNum(funeralDTO.getId(), funeralDTO.getOrderId(), "401", "12");
                if (rsts <= 0) {
                    noExistBankCertList.add(funeralDTO.getIdcard());

                }
            }
        }
        if(CollectionUtils.isNotEmpty(noExistBankCertList)){
            for(String idcard : noExistBankCertList){
                funeralDTOList.remove(idcardCheckDTOMap.get(idcard));
            }
            idcardCheckRstMsg.put("noExistBankCert", noExistBankCertList);
        }
        if(funeralDTOList.size() == 0){

            return idcardCheckRstMsg;
        }
        log.info("开始更新数据");
        List<String> ids = funeralDTOList.stream().map(FuneralDTO::getId).collect(Collectors.toList());

        funeralWriteService.updateAduit(ids,Constant.FuneralStatus.AUDIT);


        List<String> successIdcardList = funeralDTOList.stream().map(FuneralDTO::getName).collect(Collectors.toList());


        idcardCheckRstMsg.put("success", successIdcardList);

        return idcardCheckRstMsg;
    }



    @Override
    public List<String> confirmFuneralAudit(FuneralParam param, UserAccountVo accountVo) {
        List<FuneralDTO> funeralList = param.getFuneralList();
        List<String> idListYes = new ArrayList<>();
        List<String> idListNo = new ArrayList<>();
        if (CollectionUtils.isEmpty(funeralList)) {
            throw new BizException(FailureCode.ERR_10003);
        }

        if(Constant.AudtiOrApprovalResult.YES.equals(param.getCheckResult())){
            for(FuneralDTO funeralDTO : funeralList){

                String rst = fundPayReadService.verifyFundPayFuneralThrow(Constant.BusinessCode.TEKUN, accountVo.getAreaCode(),
                        funeralDTO.getStandType(), funeralDTO.getPayDate(), funeralDTO.getRecevierType(),
                        Constant.FundType.FUNERAL_FUND_PAY);

                if(StringUtils.isNotBlank(rst)){

                    idListNo.add(funeralDTO.getName());
                }else{
                    idListYes.add(funeralDTO.getId());
                }
            }
            if(!CollectionUtils.isEmpty(idListYes)){
                funeralWriteService.updateAduit(idListYes,Constant.FuneralStatus.CHECKED);
            }
        }else{
            for(FuneralDTO funeralDTO : funeralList){
                idListYes.add(funeralDTO.getId());
            }

            funeralWriteService.updateAduit(idListYes,Constant.FuneralStatus.REJECT);
        }
        if(!CollectionUtils.isEmpty(idListYes)){
            for(String id:idListYes){

                SuspendApproveEntity approveEntity = new SuspendApproveEntity();
                approveEntity.setId(Constant.getUUID());
                approveEntity.setCheckOpinion(param.getCheckOption());
                approveEntity.setCheckResult(param.getCheckResult());
                approveEntity.setCheckPeople(accountVo.getUsername());
//            approveEntity.setCheckType(param.getCheckResult());
                approveEntity.setOperator(accountVo.getUserId());
                approveEntity.setOperatorDate(new Date());
                approveEntity.setOrgId(accountVo.getOrgId());
                approveEntity.setSuspendId(id);
                suspendApproveWriteService.insertIgnoreNull(approveEntity);

            }
        }

        return idListNo;
    }


    @Override
    public List<SuspendApproveEntity> getAduitInfo(String id) {
       return suspendApproveReadService.findApproveBySuFmId(id);
    }


    @Override
    public ExcelInfo exportExcelFuneralSettle(String exportType, List<String> orderIdList, FuneralParam param, UserAccountVo accountVo) {
        ExcelInfo excelInfo = null;
        QueryParamUtil.setBaseParamValue(param,accountVo);
        param.setBusinessType(Constant.BusinessCode.TEKUN);

        List<FuneralExcelDTO> list = funeralReadService.exportExcelFuneralSettle(param);
        excelInfo = exportExcelService.exportOrgOpLogs(FuneralExcelDTO.class,list,"丧葬费查询信息.xls",
                "丧葬费查询列表","丧葬费查询列表",false);


        return excelInfo;
    }
}
